﻿(function () {
    var l = abp.localization.getResource('LanguageManagement');
    
    var _languagesAppService = yunxSoft.abp.languageManagement.language;

    var _editModal = new abp.ModalManager(
        abp.appPath + 'LanguageManagement/Languages/EditModal'
    );
    var _createModal = new abp.ModalManager(
        abp.appPath + 'LanguageManagement/Languages/CreateModal'
    );

    var _dataTable = null;

    abp.ui.extensions.entityActions.get('languageManagement.languages').addContributor(
        function(actionList) {
            return actionList.addManyTail(
                [
                    {
                        text: l('Edit'),
                        visible: abp.auth.isGranted(
                            'LanguageManagement.Languages.Update'
                        ),
                        action: function (data) {
                            _editModal.open({
                                id: data.record.id,
                            });
                        },
                    },
                    {
                        text: l('SetAsDefaultLanguage'),
                        visible: abp.auth.isGranted(
                            'LanguageManagement.Languages.ChangeDefault'
                        ),
                        action: function (data) {
                            _languagesAppService
                                .setAsDefault(data.record.id)
                                .then(function () {
                                    _dataTable.ajax.reloadEx();
                                    abp.notify.success(l('SetAsDefaultLanguage'));
                                });
                        },
                    },
                    {
                        text: l('Delete'),
                        visible: abp.auth.isGranted(
                            'LanguageManagement.Languages.Delete'
                        ),
                        confirmMessage: function (data) {
                            if (data.record.isDefaultLanguage) {
                                return l(
                                    'DefaultLanguageDeletionConfirmationMessage',
                                    data.record.displayName
                                );
                            } else {
                                return l(
                                    'LanguageDeletionConfirmationMessage',
                                    data.record.displayName
                                );
                            }
                        },
                        action: function (data) {
                            _languagesAppService
                                .delete(data.record.id)
                                .then(function () {
                                    _dataTable.ajax.reloadEx();
                                    abp.notify.success(l('DeletedSuccessfully'));
                                });
                        },
                    }
                ]
            );
        }
    );

    abp.ui.extensions.tableColumns.get('languageManagement.languages').addContributor(
        function (columnList) {
            columnList.addManyTail(
                [
                    {
                        title: l("Actions"),
                        rowAction: {
                            items: abp.ui.extensions.entityActions.get('languageManagement.languages').actions.toArray()
                        }
                    },
                    {
                        title: l("DisplayName"),
                        data: 'displayName',
                        render: function (data, type, row) {
                            if (row.isDefaultLanguage) {
                                return '<strong>' + data + '(' + l("DefaultLanguage") + ')<strong>';
                            }
                            return data
                        }
                    },
                    {
                        title: l("CultureName"),
                        data: 'cultureName',
                    },
                    {
                        title: l("UiCultureName"),
                        data: 'uiCultureName',
                    },
                    {
                        title: l("IsEnabled"),
                        data: 'isEnabled',
                        render: function (data, type, row) {
                            if (data) {
                                return '<i class="fa fa-check" aria-hidden="true"></i>';
                            } else {
                                return '<i class="fa fa-times" aria-hidden="true"></i>';
                            }
                        }
                    },
                    
                ]
            );
        },
        0 //adds as the first contributor
    );

    $(function () {
        var _$wrapper = $('#LanguagesWrapper');

        _dataTable = _$wrapper.find('table').DataTable(
            abp.libs.datatables.normalizeConfiguration({
                order: [[2, 'asc']],
                processing: true,
                paging: true,
                scrollX: true,
                serverSide: true,
                ajax: abp.libs.datatables.createAjax(_languagesAppService.getList),
                columnDefs: abp.ui.extensions.tableColumns.get('languageManagement.languages').columns.toArray(),
            })
        );

        _createModal.onResult(function () {
            _dataTable.ajax.reloadEx();
        });

        _editModal.onResult(function () {
            _dataTable.ajax.reloadEx();
        });

        $('#AbpContentToolbar button[name=CreateNewLanguage]').click(function (e) {
            e.preventDefault();
            _createModal.open();
        });
    });
})();